#include "paymentsmodel.h"

PaymentsModel::PaymentsModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}




void PaymentsModel::load_data(const QDate &qd_target_filter, const QVariant &qv_ap_index)
{
    if(qd_target_filter.isNull() && qv_ap_index.isNull())
    {
        mqvl_indices = mp_dbms->get_db(LAKDB_PAYMENTS)->findDataRange(-1);
        mqvl_indices.removeFirst();
    }
    if(qd_target_filter.isValid() && qv_ap_index.isNull() )
    {
        mqvl_indices = mp_dbms->get_db(LAKDB_PAYMENTS)->findDataEq(FLD_PAY_TARGETDATE, QVariant(qd_target_filter));
    }
    if(qd_target_filter.isNull() && qv_ap_index.isValid())
    {
        mqvl_indices = mp_dbms->get_db(LAKDB_PAYMENTS)->findDataEq(FLD_PAY_AINDEX, qv_ap_index);
    }
    if(qd_target_filter.isValid() && qv_ap_index.isValid())
    {

        SleeQHash_Constrain constr;
        constr.append(QVariant(qd_target_filter), FLD_PAY_TARGETDATE);
        constr.set_process_string("$0% = $0#");
        mqvl_indices = mp_dbms->get_db(LAKDB_PAYMENTS)->findDataEq(FLD_PAY_AINDEX, qv_ap_index,NULL,&constr);

    }
    qDebug()<<"payments loaded: "<<mqvl_indices.size();
}

int PaymentsModel::rowCount(const QModelIndex &/*parent*/) const
{
    return mqvl_indices.size();
}

int PaymentsModel::columnCount(const QModelIndex &/*parent*/) const
{
    //0 - bill number
    //1 - lease number
    //2 - apartment info
    //3 - targed_date
    //4 - payment date
    //5 - ammount
    //6 - total space

    return 7;
}

QVariant PaymentsModel::data(const QModelIndex &index, int role) const
{
    static int last_row = -1;
    static SLEE_HDB_REC qh_data;
    if((role != Qt::DisplayRole) &&
        (role!= Qt::ToolTipRole)) return QVariant();
    int r = index.row();
    int c = index.column();
    QString qstr;
    if(r != last_row)
    {
        last_row = r;
        qh_data = mp_dbms->get_data(LAKDB_PAYMENTS, mqvl_indices.at(r));
    }
    switch (c)
    {
    case 0: qstr = qh_data.value(FLD_PAY_BINDEX).toString();
        break;
    case 1:
            if(qh_data.value(FLD_PAY_LINDEX).toULongLong() == 0ll) qstr ="---";
            else qstr = qh_data.value(FLD_PAY_LINDEX).toString();
            break;
    case 2: qstr = mp_dbms->get_apartment_info(qh_data[FLD_PAY_AINDEX]);
                break;
    case 3:qstr = qh_data.value(FLD_PAY_TARGETDATE).toDate().toString("MM.yyyy");
                break;
    case 4: qstr = qh_data.value(FLD_PAY_DATE).toDate().toString("dd.MM.yyyy");
                break;
    case 5: qstr = QString::number(qh_data.value(FLD_PAY_AMMOUNT).toDouble());
                break;
    case 6: qstr = QString::number(mp_dbms->get_apartment_lspace(qh_data.value(FLD_PAY_AINDEX),
                                                                                                     qh_data.value(FLD_PAY_SPSLST).toStringList() ));
                break;
    }
    return QVariant(qstr);
}

QVariant PaymentsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if((role != Qt::DisplayRole) &&
        (role!= Qt::ToolTipRole)) return QVariant();

    if(orientation == Qt::Vertical) return QVariant(section+1);
    if(orientation != Qt::Horizontal) return QVariant();

    QString qstr;
    switch (section)
    {
    case 0: qstr = RUSTR("Номер платежа."); break;
    case 1: qstr = RUSTR("Договор №"); break;
    case 2: qstr = RUSTR("Жилое помещение"); break;
    case 3: qstr = RUSTR("Оплачиваемый период"); break;
    case 4: qstr = RUSTR("Дата оплаты"); break;
    case 5: qstr = RUSTR("Сумма (руб.)"); break;
    case 6: qstr = RUSTR("Площадь (кв.м.)"); break;
    }
    return QVariant(qstr);
}

QVariant PaymentsModel::get_db_index(const QModelIndex &index)
{
    if(!index.isValid()) return QVariant();
    return mqvl_indices.at(index.row());
}

QModelIndex PaymentsModel::get_model_index(const QVariant &qv_index)
{
    int r = mqvl_indices.indexOf(qv_index);
    if(r<0) return QModelIndex();
    return createIndex(r,0);
}
